/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package actions

import (
	"sync"
	"gitee.com/tugoer/orivil/config"
	"gitee.com/tugoer/psy-testing/code/sites/quce_la"
	"gitee.com/tugoer/orivil/mux"
	"gitee.com/tugoer/psy-testing/code/api/tags"
	"encoding/json"
)

/**
	滑动条模块数据
 */
var sliders = initSlider()

var locker = &sync.RWMutex{}

var parser = config.NewJsonParser("data")

func initSlider() (sliders []*Slider) {
	err := parser.ReadOrCreate("sliders", &sliders)
	if err != nil {
		panic(err)
	}
	return
}

func SaveSliders(ss []*Slider) error {
	locker.Lock()
	defer locker.Unlock()
	sliders = ss
	return parser.Create("sliders", sliders)
}

func GetSliders() []*Slider {
	locker.RLock()
	defer locker.RUnlock()
	return sliders
}

type Slider struct {
	Url string `json:"url"`
	Img string `json:"img"`
}

/**
专业模块数据
*/

type Professional struct {
	quce_la.List
	Url string `json:"url"`
	Typ string `json:"typ"`
}

var professionals = initProfessionals()

func initProfessionals() (professionals []*Professional) {
	err := parser.ReadOrCreate("professionals", &professionals)
	if err != nil {
		panic(err)
	}
	return
}

func SaveProfessionals(ps []*Professional) error {
	locker.Lock()
	defer locker.Unlock()
	professionals = ps
	return parser.Create("professionals", professionals)
}

func GetProfessionals() []*Professional {
	locker.RLock()
	defer locker.RUnlock()
	return professionals
}

/**
热门推荐模块数据
*/

type Recommend struct {
	Title string `json:"title"`
	Img string `json:"img"`
	Typ string `json:"typ"`
	Url string `json:"url"`
}

var recommends = initRecommends()

func initRecommends() (recommends []*Recommend) {
	err := parser.ReadOrCreate("recommends", &recommends)
	if err != nil {
		panic(err)
	}
	return
}

func SaveRecommends(ps []*Recommend) error {
	locker.Lock()
	defer locker.Unlock()
	recommends = ps
	return parser.Create("recommends", recommends)
}

func GetRecommends() []*Recommend {
	locker.RLock()
	defer locker.RUnlock()
	return recommends
}

var Advert = []*mux.Action{
	{
		Name: "Get Sliders",
		Method:"GET",
		Route: "/sliders",
		Handler: func(ctx *mux.Context) {
			ctx.SendJson(GetSliders())
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Advertisement},
			Summary:"获取滚动模块内容",
			Responses:mux.Responses{
				"200":{
					Description:"success",
					Example:[]Slider{{}},
				},
			},
		},
	},
	{
		Name: "Set Sliders",
		Method:"PUT",
		Route: "/sliders",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&putContent{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &putContent{}
			var ss []*Slider
			ctx.WarnParse(p)
			err := json.Unmarshal([]byte(p.Content), &ss)
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.ErrWarning(SaveSliders(ss))
			ctx.MsgSuccess("修改成功", "")
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Advertisement},
			Summary:"修改滚动模块内容",
		},
	},

	{
		Name: "Get Professionals",
		Method:"GET",
		Route: "/professionals",
		Handler: func(ctx *mux.Context) {
			ctx.SendJson(GetProfessionals())
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Advertisement},
			Summary:"获取专业模块内容",
			Responses:mux.Responses{
				"200":{
					Description:"success",
					Example:[]Professional{{}},
				},
			},
		},
	},
	{
		Name: "Set Professionals",
		Method:"PUT",
		Route: "/professionals",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&putContent{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &putContent{}
			ctx.WarnParse(p)
			var ss []*Professional
			err := json.Unmarshal([]byte(p.Content), &ss)
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.ErrWarning(SaveProfessionals(ss))
			ctx.MsgSuccess("修改成功", "")
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Advertisement},
			Summary:"修改专业模块内容",
		},
	},

	{
		Name: "Get Recommends",
		Method:"GET",
		Route: "/recommends",
		Handler: func(ctx *mux.Context) {
			ctx.SendJson(GetRecommends())
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Advertisement},
			Summary:"获取推荐模块内容",
			Responses:mux.Responses{
				"200":{
					Description:"success",
					Example:[]Recommend{{}},
				},
			},
		},
	},
	{
		Name: "Set Recommends",
		Method:"PUT",
		Route: "/recommends",
		Params:mux.Parameters{
			{
				In:mux.Form,
				Schema:&putContent{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &putContent{}
			ctx.WarnParse(p)
			var ss []*Recommend
			err := json.Unmarshal([]byte(p.Content), &ss)
			if err != nil {
				ctx.ErrWarning(err)
			}
			ctx.ErrWarning(SaveRecommends(ss))
			ctx.MsgSuccess("修改成功", "")
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tags.Advertisement},
			Summary:"修改推荐模块内容",
		},
	},
}


type putContent struct {
	Content string `desc:"json 格式字符串"`
}
